@using System.Text
@using MySql.Data.MySqlClient
@using Npgsql
@using SmartCode
@using SmartCode.Db
@model BuildContext
@{
    var project = Model.Project;
    var buildTask = Model.Build;
    var appName = "Demo";
    if (project.Parameters.TryGetValue("ApplicationName", out var _appName))
    {
      appName = _appName.ToString();
    }
    var appClassName = $"{appName}Application";
    var dbSource = Model.GetDataSource<DbSource>();
    if (!project.BuildTasks.TryGetValue("Entity", out var entityBuild))
    {
      throw new ArgumentException("can not find Project -> BuildTasks -> [Entity] .");
    }

  var driveName = "mysql";
  var host = "localhost";
  uint port = 3306;
  var userName = "root";
  var pwd = "root";
  var database = "smartsql_db";
  var appendUrl = new StringBuilder();
  
  switch (dbSource.DbProvider.Name)
  {
    case SmartSql.DataSource.DbProvider.MYSQL:
    {
      driveName = "mysql";
      var builder= new MySqlConnectionStringBuilder(dbSource.WriteDataSource.ConnectionString);
      host = builder.Server;
      database= builder.Database;
      userName= builder.UserID;
      pwd = builder.Password;
      port = builder.Port;
      appendUrl.Append("?");
      appendUrl.Append("serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8");
      break;
    }
    case SmartSql.DataSource.DbProvider.POSTGRESQL:
    {
      driveName = "postgresql";
      var builder= new NpgsqlConnectionStringBuilder(dbSource.WriteDataSource.ConnectionString);
      host = builder.Host;
      database= builder.Database;
      userName= builder.Username;
      pwd = builder.Password;
      port = (uint)builder.Port;
      break;
    }
  }
}

spring:
  application:
    name: @appName-service

  datasource:
    url: jdbc:@(driveName)://@host:@port/@database@appendUrl.ToString()
    username: @userName
    password: @pwd

mybatis:
  mapper-locations: classpath:/mappers/*.xml
  type-aliases-package: @(project.Module).@(entityBuild.Module)
  configuration:
    map-underscore-to-camel-case: true